package cn.edu.zhku.service.impl;

import cn.edu.zhku.pojo.User;
import cn.edu.zhku.mapper.UserMapper;
import cn.edu.zhku.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findById(Integer userId) {
        return this.userMapper.selectById(userId);
    }

    @Override
    @Transactional
    public void addOne(User user) {
        this.userMapper.insertOne(user);
    }

    @Override
    @Transactional
    public void modifyOne(User user) {
        this.userMapper.updateOne(user);
    }

    @Override
    @Transactional
    public void resetPwdById(Integer userId, String password, String salt) {
        this.userMapper.updatePwdAndSaltById(userId, password, salt);
    }

    @Override
    public User findUserRoleMenuByUserId(Integer userId) {
        return this.userMapper.selectUserRoleMenuByUserId(userId);
    }

    @Override
    public boolean checkUserExistByUsername(String username) {
        return this.userMapper.selectUserNumByUsername(username) == 1;
    }

    @Override
    public User findPasswordAndSaltByUsername(String username) {
        return this.userMapper.selectPasswordAndSaltByUsername(username);
    }

    @Override
    public User findPasswordAndSaltById(Integer userId) {
        return this.userMapper.selectPasswordAndSaltById(userId);
    }

    @Override
    public boolean checkUserExistByNo(String no) {
        return this.userMapper.selectUserNumByNo(no) == 1;
    }
}
